package yxy.game.pm2.command.guard;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.GuardMessage;
import yxy.game.pm2.module.guard.GuardHandle;
import yxy.game.pm2.module.guard.GuardPlayerModule;

/** 洛阳守卫 洛阳守卫主界面 */
@MessageType(proto = Command.GuardUI, classtype = GuardMessage.GuardUIRequest.class)
public class CMD_GuardUI extends Command<GuardMessage.GuardUIRequest> {

	@Override
	public void execute(MessageSource messageSource, GuardMessage.GuardUIRequest request) {
		String playerCode = messageSource.getTag();

		GuardMessage.GuardUIResponse.Builder response = GuardMessage.GuardUIResponse.newBuilder();

		RedisLock lockp = GuardPlayerModule.lock(playerCode);
		try {
			if (lockp.tryLock()) {
				new GuardHandle().info(playerCode, response);

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			lockp.unlock();
		}
	}

	private void send(MessageSource messageSource, GuardMessage.GuardUIResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}